package com.compass.mobile.network.interceptor

import UserInfoManager
import com.compass.mobile.framework.constant.KEY_SMS_LOGIN
import com.compass.mobile.framework.constant.YD_USER_ID
import com.compass.mobile.framework.constant.YD_USER_TOKEN
import okhttp3.Interceptor
import okhttp3.Response

/**
 * @author mingyan.su
 * @date   2023/3/27 07:25
 * @desc   头信息拦截器
 * 添加头信息
 */
class HeaderInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val newBuilder = request.newBuilder()
        newBuilder.addHeader("Content-type", "application/json; charset=utf-8")
        val url = request.url().toString()
        //判断那些接口不需要加token 反之添加
        if (!url.contains(KEY_SMS_LOGIN)) {
            newBuilder.addHeader("Authorization","Bearer ${UserInfoManager.getAccessToken()}")
        }
        UserInfoManager.getHospitalId()?.let {
            newBuilder.addHeader("hospital-id",it)
        }
//        newBuilder.addHeader("Platform-Id","1796013154879082498")
        newBuilder.addHeader("yd-user-id",UserInfoManager.getString(YD_USER_ID))
        //注册设备来源 WECHAT_MINI_PROGRAM(10, "微信小程序"), WECHAT_WAP(11, "微信公众号"), H5(20, "H5 网页"), APP(31, "手机 App"),
        newBuilder.addHeader("terminal", "31")
        return chain.proceed(newBuilder.build())
    }
}